package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree;


import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Hand;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Seat;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Suit;

public final class MajorMinorDistributionFilter extends DistributionFilterGroupItem {
    private CardsCountInterval majorCardsCountInterval = CardsCountInterval.create(4, 5, false);
    private CardsCountInterval minorCardsCountInterval = CardsCountInterval.create(4, 5, false);
    private Seat seat = Seat.NORTH;

    public CardsCountInterval getMajorCardsCountInterval() {
        return majorCardsCountInterval;
    }

    public void setMajorCardsCountInterval(CardsCountInterval majorCardsCountInterval) {
        if (majorCardsCountInterval == null) {
            throw new NullPointerException();
        }
        this.majorCardsCountInterval = majorCardsCountInterval;
    }

    public CardsCountInterval getMinorCardsCountInterval() {
        return minorCardsCountInterval;
    }

    public void setMinorCardsCountInterval(CardsCountInterval minorCardsCountInterval) {
        if (minorCardsCountInterval == null) {
            throw new NullPointerException();
        }
        this.minorCardsCountInterval = minorCardsCountInterval;
    }

    public Seat getSeat() {
        return seat;
    }

    public void setSeat(Seat seat) {
        this.seat = seat;
    }

    protected boolean matchesInternal(Distribution distribution) {
        Hand hand = distribution.getHand(seat);
        return checkMajor(hand) && checkMinor(hand);
    }


    private boolean checkMajor(Hand hand) {
        return majorCardsCountInterval.contains(hand.getCardsCount(Suit.SPADE)) ||
                majorCardsCountInterval.contains(hand.getCardsCount(Suit.HEART));
    }

    private boolean checkMinor(Hand hand) {
        return minorCardsCountInterval.contains(hand.getCardsCount(Suit.DIAMOND)) ||
                minorCardsCountInterval.contains(hand.getCardsCount(Suit.CLUB));
    }


    @Override
    public boolean isCopy(GroupItem groupItem) {
        if (!super.isCopy(groupItem)) return false;
        if (!(groupItem instanceof MajorMinorDistributionFilter)) return false;

        MajorMinorDistributionFilter that = (MajorMinorDistributionFilter) groupItem;
        return seat == that.seat && majorCardsCountInterval.equals(that.majorCardsCountInterval) && minorCardsCountInterval.equals(that.minorCardsCountInterval);
    }

    @Override
    public MajorMinorDistributionFilter copy() {
        MajorMinorDistributionFilter copy = new MajorMinorDistributionFilter();
        copy.majorCardsCountInterval = this.majorCardsCountInterval.copy();
        copy.minorCardsCountInterval = this.minorCardsCountInterval.copy();
        copy.seat = this.seat;
        return copy;
    }
}

